<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/15
 * Time: 16:06
 */

namespace backend\controllers;


use common\controllers\BackendCommonCtrl;
use common\services\WorksClassSve;
use Yii;
use common\library\comFun;
use yii\helpers\Url;

class WorksClassController extends BackendCommonCtrl
{
    /**
     * 蛋糕分类列表
     * @return string
     */
    public function actionList()
    {
        $WorksClassSve = new WorksClassSve();
        $rs = $WorksClassSve->getList([], 'sort DESC,works_class_id ASC', true);
        return $this->render('list', $rs['data']);
    }

    /**
     * 添加蛋糕分类
     * @return string
     */
    public function actionAdd()
    {
        return $this->render('edit', ['info' => []]);
    }

    /**
     * 编辑蛋糕分类
     * @return string|\yii\web\Response
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $WorksClassSve = new WorksClassSve();
        if ($request->isPost) {
            if ($request->post('works_class_id')) {
                $rs = $WorksClassSve->update($request->post());
            } else {
                $rs = $WorksClassSve->add($request->post());
            }
            if ($rs['code'] === 0) {
                $rs['redirect'] = Url::toRoute('/works-class/list');
            }
            return $this->asJson($rs);
        } else {
            $id = $request->get('works_class_id');
            $data = ['Obj' => []];
            //检查id
            if (comFun::isPositiveInt($id)) {
                $rs = $WorksClassSve->getInfo(['works_class_id' => $id]);
                if ($rs['code'] === 0) {
                    $data = $rs['data'];
                }
            }
            return $this->render('edit', ['info' => $data['Obj']]);
        }
    }
}